package com.bugman.managed;

import com.bugman.business.BugService;
import com.bugman.business.CommentService;
import com.bugman.entity.Bug;
import com.bugman.entity.Comment;
import com.bugman.entity.Importance;
import com.bugman.entity.Role;
import com.bugman.entity.Status;
import com.bugman.entity.User;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.model.SelectItem;

/**
 *
 * @author Ediczech
 */
public class BugDetailBean extends BaseBean {

    final private Integer COUNT = 10;
    private Byte importanceId;
    private Byte statusId;
    private Bug bug;
    private Comment comment;
    private ResourceBundle rb = getBundle();
    private List<Comment> comments;
    private List<SelectItem> importances;
    private List<SelectItem> statuses;
    @EJB
    BugService bugService;
    @EJB
    CommentService commentService;

    @PostConstruct
    public void init() {
        String bugId = getRequestParameter("id");
        if (bugId == null) {
            bugId = getRequestParameter("id2");
        }
        if (bugId != null) {
            bug = bugService.getBug(Integer.valueOf(bugId));
            comments = commentService.getLastComments(bug, COUNT);
            importanceId = bug.getImportance().getId();
            statusId = bug.getStatus().getId();
        }
        comment = new Comment();

        importances = new ArrayList<SelectItem>();
        for (Importance importance : bugService.getAllImportances()) {
            importances.add(new SelectItem(importance.getId(), importance.getName()));
        }
        statuses = new ArrayList<SelectItem>();
        for (Status status : bugService.getAllStatuses()) {
            statuses.add(new SelectItem(status.getId(), status.getName()));
        }

    }

    public String updateBug() {
        bug.setImportance(bugService.getImportance(importanceId));
        bug.setStatus(bugService.getStatus(statusId));
        bugService.updateBug(bug);
        comment.setText("");
        getFacesContext().addMessage("id",
                new FacesMessage(FacesMessage.SEVERITY_INFO, null,
                rb.getString("bugDetail_updateBug_success_detail")));
        logger.info("bug zmenen " + bug);
        return "success_update";
    }

    public String addComment() {
        comment.setAuthor(getLoggedUser());
        comment.setBug(bug);
        commentService.createComment(comment);
        comments = commentService.getLastComments(bug, COUNT);

        getFacesContext().addMessage("id2",
                new FacesMessage(FacesMessage.SEVERITY_INFO, null,
                rb.getString("addComment_success_detail")));
        logger.info("Pridan komentar: " + comment);
        return "success_add";

    }

    public boolean isIsBugProjectDeveloper() {
        if (!isUserLogged()) {
            return false;
        }
        List<User> bugDevelopers = bug.getProject().getDevelopers();
        Integer loggedUserId = getLoggedUser().getId();
        if (isUserInRole(String.valueOf(Role.DEVELOPER)) && loggedUserId == bug.getFinder().getId()) {
            return true;
        }
        for (User bd : bugDevelopers) {
            if (bd.getId() == loggedUserId) {
                return true;
            }
        }
        return false;
    }

    public boolean isIsAnyComment() {
        if (commentService.getCommentsCount(bug) == 0) {
            return false;
        } else {
            return true;
        }

    }

    public Bug getBug() {
        return bug;
    }

    public void setBug(Bug bug) {
        this.bug = bug;
    }

    public Comment getComment() {
        return comment;
    }

    public void setComment(Comment comment) {
        this.comment = comment;
    }

    public List<Comment> getComments() {
        return comments;
    }

    public void setComments(List<Comment> comments) {
        this.comments = comments;
    }

    public Byte getImportanceId() {
        return importanceId;
    }

    public void setImportanceId(Byte importanceId) {
        this.importanceId = importanceId;
    }

    public Byte getStatusId() {
        return statusId;
    }

    public void setStatusId(Byte statusId) {
        this.statusId = statusId;
    }

    public List<SelectItem> getImportances() {
        return importances;
    }

    public void setImportances(List<SelectItem> importances) {
        this.importances = importances;
    }

    public List<SelectItem> getStatuses() {
        return statuses;
    }

    public void setStatuses(List<SelectItem> statuses) {
        this.statuses = statuses;
    }
}
